import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;

/**
 * Class used to Parse the parse containing individual professor data
 * @author Nick
 *
 */
public class ProfessorDataPage {
	//the root node of the xml tree
	private TagNode tagNode;
	
	public ProfessorDataPage(String url) throws MalformedURLException, IOException{
		CleanerProperties props = new CleanerProperties();
		 
		//HTML cleaner properties
		props.setTranslateSpecialEntities(true);
		props.setTransResCharsToNCR(true);
		props.setOmitComments(true);
		
		
		//download page, parses to html
		tagNode = new HtmlCleaner(props).clean(
			    new URL(url)
		);
		
		//writes to file
		new PrettyXmlSerializer(props).writeToFile(
			    tagNode, "testBanana.xml", "utf-8"
			);
	}
	
	/*
	 * Takes object and parses professor data from it
	 */
	public ProfessorModel gatherInitialInformation() throws MalformedURLException, IOException{
		
		//finds individual info section
		TagNode indInfo = findTagNodeForName(tagNode,"div","class","ind_info");

		//navigates to prof info section
		TagNode mainInfo = getChildByNumber(indInfo,0);
		TagNode indName = getChildByNumber(mainInfo,0);
		TagNode profinfo= getChildByNumber(indInfo,2);
		
	TagNode emailInfo;
	TagNode indEmail;
	String profEmail=null;
	TagNode nodeS;
	//searches for prof email in any sub section of ind info
	if((findTagNodeForName(indInfo,"div","class","sub_title sub_email" ))!=(null)){
			emailInfo=findTagNodeForName(indInfo,"div","class","sub_title sub_email" );
			nodeS = getChildByNumber(emailInfo.getParent(),1);
				indEmail = getChildByNumber(nodeS,1);
				profEmail = indEmail.getText().toString();
	}

	TagNode office=getChildByNumber(profinfo, 1);
	TagNode phoneInfo=null;
	//searches for phone # in any sub node of ind info
	if(findTagNodeForName(profinfo, "div", "class", "sub_phone")!=(null)){
		phoneInfo=findTagNodeForName(profinfo, "div", "class", "sub_phone");
		
		
	}
	
		String profName = indName.getText().toString();
		String profOffice = office.getText().toString();
		String profPhone=phoneInfo.getText().toString();
		
		ProfessorModel prof = new ProfessorModel(profName,profEmail, profOffice, profPhone);
		System.out.println("name: " + profName);
		System.out.println("email: " + profEmail);
		System.out.println("office: " + profOffice);
		System.out.println("phone: " + profPhone);
		//returnes professor info
		return prof;

	}
	//helper method
	public static TagNode findTagNodeForName(TagNode node,String name,String attribute_name, String attribute_value){
			if(node.getName() != null && node.getName().equals(name)){
			//	System.out.println("Looking into attributes for :" + node.getName());
				Map<String,String> attributes = node.getAttributes();
				Set<String> keySet = attributes.keySet();
			
				for(String key: keySet){
					if(key.equals(attribute_name) && attributes.get(key).equals(attribute_value)){
						return node;
					}
				}
			}
		
			TagNode[] tags = node.getChildTags();
			for(TagNode tag:tags){
				TagNode newTag = findTagNodeForName(tag,name,attribute_name, attribute_value);
				if(newTag != null){
					return newTag;
				}
			}
		return null;
	
	}

	public static TagNode getChildByNumber(TagNode parent,int number){
		TagNode[] childTags = parent.getChildTags();
		return childTags[number];
	
	}
	
}
